package com.guge.sports.toolPackage.aop;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;

import com.guge.sports.utils.R;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.HashMap;

/**
 * 主要用于验证Controller是否合法
 */
@EnableAspectJAutoProxy
@Aspect
@Component
public class VerificationAop {


    /*@Pointcut("execution(* *..*.controller.select*(Object))")
    public void select(){}

    @Pointcut("execution(* *..*.controller.update*(Object))")
    public void update(){}

    @Pointcut("execution(* *..*.controller.insert*(Object))")
    public void insert(){}

    @Pointcut("execution(* *..*.controller.delete*(Object))")
    public void delete(){}*/

    //&& @annotation(org.springframework.web.bind.annotation.RequestMapping)
    @Pointcut("execution(public Object com.guge.sports.toolPackage.base.BaseController.*(com.baomidou.mybatisplus.extension.activerecord.Model))")
    public void entityPointCut() {
    }

    /*@Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
    public void mappingPointCut() { }*/
    //&& @annotation(org.springframework.web.bind.annotation.RequestMapping)
    @Around("entityPointCut()")
    public Object myAround(ProceedingJoinPoint pj) throws Throwable {
        System.out.println("环绕前增强");
        Object[] args = pj.getArgs();
        if (checkObjAllFieldsIsNull(args[0])) {
            //参数全为空
            throw new IllegalArgumentException("参数全为空");
            //return sendJsonResult(false);
        }
        Object result = pj.proceed();
        //执行run方法
        System.out.println("环绕后增强");
        System.out.println(result);
        if (result == null) {
            return R.error().message("返回null");
        }

        if (result instanceof Boolean) {
            boolean b = (Boolean) result;
            return R.result(b);
        }

       /* if (result instanceof List && ((List) result).size() == 0) {
            return sendJsonResult(false);
        }*/
        return result;
    }

    /**
     * 检查对象的属性是否全为null或者""
     *
     * @param object
     * @return
     */
    public boolean checkObjAllFieldsIsNull(Object object) {
        if (null == object) {
            return true;
        }
        try {
            for (Field f : object.getClass().getDeclaredFields()) {
                f.setAccessible(true);
                System.out.print(f.getName() + ":");
                System.out.println(f.get(object));

                if (f.get(object) != null && StringUtils.isNotEmpty(f.get(object).toString())) {
                    return false;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

   /* *//**
     * 返回json格式信息
     *
     * @param b
     * @throws
     *//*
    private R sendJsonResult(boolean b) {
        if (b) {
            hashMap.put("state", "success");
        } else {
            hashMap.put("state", "error");
        }
        return hashMap;
    }*/

}
